﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AdvanceTimeTableManagement.Manager;

namespace AdvanceTimeTableManagement.AdminModule
{
    public partial class ClassSetUp : Form
    {
        public bool IsUpdate { get; set; }
        public string ClassCodeToUpdate { get; set; }

        public ClassSetUp()
        {
            InitializeComponent();
            btnAddClass.Enabled = false;
            if (IsUpdate)
            {
                txtCode.Enabled = false;
            }
            else
            {
                txtCode.Enabled = true;
            }
        }

        public void GetClassData()
        {
            using (TimeTableDataContext dataContext = new TimeTableDataContext())
            {
                txtCode.Text = ClassCodeToUpdate;
                List<ClassSetup> classItems = dataContext.ClassSetups.Where(obj => (obj.ClassCode == ClassCodeToUpdate)).ToList();
                txtName.Text = classItems[0].ClassName;
                txtDescription.Text = classItems[0].Description;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!IsUpdate)
            {
                using (TimeTableDataContext dataContext = new TimeTableDataContext())
                {
                    ClassSetup classitem = new ClassSetup()
                    {
                        ClassCode = txtCode.Text,
                        ClassName = txtName.Text,
                        Description = txtDescription.Text,
                        CreatedDate = DateTime.Now
                    };

                    dataContext.ClassSetups.InsertOnSubmit(classitem);
                    dataContext.SubmitChanges();
                    txtCode.Text = "";
                    txtName.Text = "";
                    txtDescription.Text = "";
                }
            }
            else
            {
                using (TimeTableDataContext dataContext = new TimeTableDataContext())
                {
                    List<ClassSetup> classItems = dataContext.ClassSetups.Where(obj => (obj.ClassCode == ClassCodeToUpdate)).ToList();
                    if (classItems.Count > 0)
                    {
                        classItems[0].ClassName = txtName.Text;
                        classItems[0].Description = txtDescription.Text;
                        dataContext.SubmitChanges();
                    }

                    ClassSetUpList formShow = new ClassSetUpList();
                    formShow.Show();
                    this.Hide();
                }
            }
        }

        private void btnAddClass_Click(object sender, EventArgs e)
        {

        }

        private void btnClassList_Click(object sender, EventArgs e)
        {
            ClassSetUpList formShow = new ClassSetUpList();
            formShow.Show();
            this.Hide();
        }
    }
}
